package demo;

import java.util.Random;

public class test4 {
    //需求：生成验证码
    public static void main(String[] args) {
        //1.随机生成五个字母的字符串
        //1.1创建字母数组
        char[] arr=new char[52];
        for (int i = 0; i < 26; i++) {
            arr[i]=(char)(65+i);
        }
        for (int i = 26; i < 52; i++) {
            arr[i]=(char)(97+i-26);
        }
        //2.随机生成4个字母的字符数组
        Random r=new Random();
        char[] str=new char[5];
        for (int i = 0; i < 4; i++) {
            int pos=r.nextInt(52);
            str[i]=arr[pos];
        }
        //3.随机生成数字拼接到末尾
        int num=r.nextInt(10);
        //3.1把数字转换成字符
        char c=(char)(num+48);
        str[4]=c;
        //4.打乱字符数组
        for (int i = 0; i < str.length; i++) {
            int pos=r.nextInt(str.length);
            char tmp=str[pos];
            str[pos]=str[i];
            str[i]=tmp;
        }
        //5.字符数组转换成字符串
        String sttr=new String(str);
        //2.打印
        System.out.println(sttr);
    }
}
